﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommandLine;
using CommandLine.Text;
using System.Text.RegularExpressions;

namespace TCP_UDP_client_server_test
{
    
    class Program
    {
        private enum Role { client, server };
        private enum Proto { tcp, udp };

        private static Role clientOrServer = Role.client;
        private static Proto tcpOrUdp = Proto.tcp;

        static void Main(string[] args)
        {
            bool continueTest = true;

            while (continueTest)
            {
                bool acquireChoice = false;
                while (!(acquireChoice))
                {
                    Console.Write("Specify whether we're server or client [{0}]: ", clientOrServer.ToString());
                    string roleInput = Console.ReadLine().Trim().ToLower();

                    if (roleInput.Length > 0)
                    {
                        switch (roleInput)
                        {
                            case "client":
                                clientOrServer = Role.client;
                                acquireChoice = true;
                                break;
                            case "c":
                                clientOrServer = Role.client;
                                acquireChoice = true;
                                break;
                            case "server":
                                clientOrServer = Role.server;
                                acquireChoice = true;
                                break;
                            case "s":
                                clientOrServer = Role.server;
                                acquireChoice = true;
                                break;
                            default:
                                break;
                        }
                          
                    }
                    else
                    {
                        acquireChoice = true;
                    }
                }

                acquireChoice = false;
                while (!(acquireChoice))
                {
                    Console.Write("Specify TCP or UDP [{0}]: ", tcpOrUdp.ToString());
                    string protoInput = Console.ReadLine().Trim().ToLower();

                    if (protoInput.Length > 0)
                    {
                        switch (protoInput)
                        {
                            case "udp":
                                tcpOrUdp = Proto.udp;
                                acquireChoice = true;
                                break;
                            case "tcp":
                                tcpOrUdp = Proto.tcp;
                                acquireChoice = true;
                                break;
                            case "u":
                                tcpOrUdp = Proto.udp;
                                acquireChoice = true;
                                break;
                            case "t":
                                tcpOrUdp = Proto.tcp;
                                acquireChoice = true;
                                break;
                            default:
                                break;
                        }
                    }
                    else
                    {
                        acquireChoice = true;
                    }
                }


                switch (clientOrServer)
                {
                    case Role.server:
                        switch (tcpOrUdp)
                        {
                            case Proto.tcp:

                                TcpServer.listen();
                                break;
                            case Proto.udp:
                                UDPServer.listen();
                                break;
                            default:

                                break;

                        }
                        break;
                    case Role.client:
                        switch (tcpOrUdp)
                        {
                            case Proto.tcp:
                                TcpTestClient.connect();
                                break;
                            case Proto.udp:
                                UdpClientTest.Connect();
                                break;
                            default:

                                break;
                        }
                        break;
                    default:
                        break;
                }

                //acquireChoice = false;
                //while( !(acquireChoice) )
                //{
                //    Console.Write("Run it again?[y]");
                //    string doneInput = Console.ReadLine().Trim().ToLower();

                //    if (doneInput.Length > 0)
                //    {
                //        switch (doneInput)
                //        {
                //            case "yes":
                //                acquireChoice = true;
                //                break;
                //            case "y":
                //                acquireChoice = true;
                //                break;
                //            case "no:":
                //                return;
                //                break;
                //            case "n":
                //                return;
                //                break;
                //            default:
                //                break;
                //        }
                //    }
                //    else
                //    {
                //        acquireChoice = true;
                //    }
                
                //}
                continueTest = false;
            }
        }
    }
}
